package com.sdk.utill;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.Generated;

public final class DYUtil {
   public static String signature(Map<String, String> header, String bodyStr) throws Exception {
      List<String> keyList = new ArrayList(4);
      header.forEach((keyx, val) -> keyList.add(keyx));
      Collections.sort(keyList, String::compareTo);
      List<String> kvList = new ArrayList(4);

      for(String key : keyList) {
         kvList.add(key + "=" + header.get(key));
      }

      String urlParams = String.join("&", kvList);
      String rawData = urlParams + bodyStr + "ljxy";
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(rawData.getBytes(StandardCharsets.UTF_8));
      return Base64.getEncoder().encodeToString(md.digest());
   }

   
   private DYUtil() {
      throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
   }
}
